package com.umbrella;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SimplePropertyPreFilter;

public class BaseController {
	private final Logger logger = LoggerFactory.getLogger(BaseController.class);
	public static final String contentType = "application/json;charset=utf-8";
	public static final String accessAllow = "*";

	protected void writeOutputJson(Result result, SimplePropertyPreFilter filter, HttpServletResponse response){

		String resultJson = JSON.toJSONString(result, filter);
		try {
			response.setContentType(contentType);
			response.setHeader("Access-Control-Allow-Origin", accessAllow);
			response.getWriter().write(resultJson);
		} catch (Exception e) {

			logger.error("writeOutputJson failed", e);
		}
	}

	protected void writeOutputJson(Result result, SimplePropertyPreFilter[] filters, HttpServletResponse response){

		String resultJson = JSON.toJSONString(result, filters);
		try {
			response.setContentType(contentType);
			response.setHeader("Access-Control-Allow-Origin", accessAllow);
			response.getWriter().write(resultJson);
		} catch (Exception e) {

			logger.error("writeOutputJson failed", e);
		}
	}
	
	protected void writeOutputJson(Result result, HttpServletResponse response){

		//String resultJson = JSON.toJSONString(result,SerializerFeature.WriteMapNullValue);
		String resultJson = JSON.toJSONString(result);
		try {
			response.setContentType(contentType);
			response.setHeader("Access-Control-Allow-Origin", accessAllow);
			response.getWriter().write(resultJson);
		} catch (Exception e) {

			logger.error("writeOutputJson failed", e);
		}
	}
	
	protected void writeOutputJsonWithoutNull(Result result, HttpServletResponse response){

		String resultJson = JSON.toJSONString(result);
		try {
			response.setContentType(contentType);
			response.setHeader("Access-Control-Allow-Origin", accessAllow);
			response.getWriter().write(resultJson);
		} catch (Exception e) {

			logger.error("writeOutputJson failed", e);
		}
	}
	protected void writeOutputJson(ResultPage result, HttpServletResponse response){
		String resultJson = JSON.toJSONString(result);//,SerializerFeature.WriteMapNullValue);
		try {
			response.setContentType(contentType);
			response.setHeader("Access-Control-Allow-Origin", accessAllow);
			response.getWriter().write(resultJson);
		} catch (Exception e) {
			
			logger.error("writeOutputJson failed", e);
		}	
	}
	
	protected String getAuthToken(HttpServletRequest request) {

		String umbraToken = request.getParameter("umbraToken");
		if (umbraToken == null) {
			umbraToken = getCooikeVal(request, "umbraToken");
		}
		if(umbraToken == null){
			umbraToken = getHeaderVal(request, "umbraToken");
		}
		return umbraToken;

	}
	
	protected String getHeaderVal(HttpServletRequest request, String name){
		
		return request.getHeader(name);
	}
	
	protected  String getCooikeVal(HttpServletRequest request, String name) {

		Cookie[] cookies = request.getCookies();
		if(cookies != null){
			for (Cookie temp : cookies) {
				if (name.equals(temp.getName())) {
					return temp.getValue();
				}
			}			
		}


		return null;
	}
	

	public void setCookieToken(HttpServletResponse response, String umbraToken) {
		Cookie cookie = new Cookie("umbraToken", umbraToken);
		response.addCookie(cookie);
	}
}
